<?php
    /**
     * Created by JetBrains PhpStorm.
     * User: porterzhang
     * Date: 13-5-28 下午2:23
     * To change this template use File | Settings | File Templates.
     */
    if (!defined ('IN_MOBILE_API')) {
        exit('Access Denied');
    }
    include_once 'forum.php';

    class cloud_api {
        function common () {
        }

        function output () {
            //判断是否缺少参数
            if (empty($_GET['caid']) || empty($_GET['udid']) ) {
                $code = '';
                if (empty($_GET['caid'])) {
                    $code = 1026;
                }
                else if (empty($_GET['udid'])) {
                    $code = 1027;
                }
                $variable = array(
                    'code' => $code, 'messagestr' => lang ('plugin/cloud', '' + $code),);
                cloud_core::result (cloud_core::variable ($variable));
            }

            $province = '';
            $city = '';
            if (!empty($_GET['longitude']) && !empty($_GET['latitude'])) {
                //通过百度的map的api得到省和市
                $url = 'http://api.map.baidu.com/geocoder/v2/?ak=69ded0fbe1c5b68f1ddc628eb58dfbe2&callback=renderReverse&location=' . $_GET['latitude'] . ',' . $_GET['longitude'] . '&output=json&pois=0';
                if ($stream = fopen ($url, 'r')  ) {
                    $jsonstring = stream_get_contents ($stream,-1);
                    $jsonstring = str_replace ('renderReverse&&renderReverse(','', $jsonstring);
                    $jsonstring = str_replace (')', '', $jsonstring);
                    require_once 'source/plugin/cloud/json.class.php';
                    $address = CJSON::decode (stripslashes ($jsonstring), true);
                    $ip_address = $address['result']['addressComponent'];
                    $province = $ip_address['province'];
                    $city = $ip_address['city'];
                    fclose ($stream);
                }
            }

            $data_value = C::t ('#cloud#cloud_ios_push')->fetch_push_info_by_udid ($_GET['udid'],$_GET['caid']);
            if (empty($data_value)) {
                $data = array(
                    'caid' => $_GET['caid'],
                    'cuid' => empty($_GET['cuid']) ? '' : $_GET['cuid'],
                    'udid' => $_GET['udid'],
                    'lasttime' => TIMESTAMP,
                    'token' => empty($_GET['token']) ? '' : $_GET['token'],
                    'versions' => empty($_GET['versions']) ? '' :$_GET['versions'],
                    'platform' => empty($_GET['platform']) ? '' :$_GET['platform'],
                    'longitude' => empty($_GET['longitude']) ? '' : $_GET['longitude'],
                    'latitude' => empty($_GET['latitude']) ? '' : $_GET['latitude'],
                    'province' => $province,
                    'city' => $city,
                    'dev' => $_GET['dev'],
                    'operator' => empty($_GET['operator']) ? '' :$_GET['operator'],
                    'brand' => empty( $_GET['brand'])?'apple':$_GET['brand'],
                );
                C::t ('#cloud#cloud_ios_push')->insert ($data);
            }
            else {
                $data = array(
                    'caid' => $_GET['caid'],
                    'cuid' => empty($_GET['cuid']) ? $data_value['cuid'] : $_GET['cuid'],
                    'udid' => $_GET['udid'],
                    'lasttime' => TIMESTAMP,
                    'token' => empty($_GET['token']) ? $data_value['token'] : $_GET['token'],
                    'versions' => empty($_GET['versions']) ? $data_value['versions'] : $_GET['versions'],
                    'platform' => empty($_GET['platform']) ?  $data_value['platform'] :$_GET['platform'],
                    'longitude' => empty($_GET['longitude']) ? $data_value['longitude'] : $_GET['longitude'],
                    'latitude' => empty($_GET['latitude']) ? $data_value['latitude'] : $_GET['latitude'],
                    'province' => empty($province) ? $data_value['province'] : $province,
                    'city' => empty($city) ? $data_value['city'] : $city,
                    'dev' => $_GET['dev'],
                    'operator' => empty($_GET['operator']) ? $data_value['operator'] :$_GET['operator'],
                    'brand' => empty( $_GET['brand'])?'apple':$_GET['brand'],
                );
                C::t ('#cloud#cloud_ios_push')->update ($data_value['id'],$data);
            }
            //正常返回
            $variable = array(
                'messagestr' => '',
            );
            cloud_core::result (cloud_core::variable ($variable));
        }
    }

?>

